package parsers.calendar;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import parsers.CommonMethods;

import model.CalendarList;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;

public class MainCalendarsParser
{	
	public MainCalendarsParser() {}
	
	public ArrayList<CalendarList> parse(URL url) throws IOException
	{
		ArrayList<CalendarList> calendars = new ArrayList<CalendarList>();
		
		Source source = new Source(url);		
		List<Element> elements = source.getAllElements(HTMLElementName.TR);
		
		for (Element element : elements)
		{
			String name = element.getContent().toString();
			
			if(name.contains("kalendarze_lista_szczegoly"))
			{
				List<Element> linkElements = element.getAllElements(HTMLElementName.TD);
				
				String link = linkElements.get(0).getContent().getFirstElement().getAttributeValue("href");
				String calendarName = linkElements.get(0).getContent().getFirstElement().getContent().toString();
				String calendarDescription = linkElements.get(1).getContent().toString();
				String calendarwarning = CommonMethods.checkSpace(linkElements.get(2).getContent().toString());
				String calendarShare = linkElements.get(3).getContent().toString().replace("<b>", "").replace("</b>", "");
				
				CalendarList mainCalendar = new CalendarList(calendarName, calendarDescription, calendarwarning, calendarShare);
				mainCalendar.setLink(CommonMethods.parseLink(link));
				calendars.add(mainCalendar);
			}
		}

		return calendars;
	}
	
	/*public static void main(String[] args) //throws MalformedURLException, IOException
	{
//		CalendarsParser cp = new CalendarsParser();
//		ArrayList<MainCalendar> list = cp.parse(new URL("http://plan.uz.zgora.pl/kalendarze_lista.php"));
//		System.out.println(list);
		
		String d = "D/";
		System.out.println(d.split("-")[1]);
	}*/
}
